﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DICE_GS;

public class BattleMateListManager : LTEventListenerC {
	List<FriendListItem> itemList = new List<FriendListItem>();
	public PvPMakeRoomSceneManager manager;
	
	// Use this for initialization
	void Start () {
		if( MyInfo.Instance.MyFriendList.Count == 0 )
		{
			LoadingWindow.window.ShowShortLoading();
			RestAPI.singleton.GetMyFriendList(ReturnCallback);
		}
		else
		{
			RefreshFriendList();
		}
	}
	
	void RefreshFriendList()
	{
		int index = 0;
		//add play alone button
		foreach(FriendInfo friendInfo in MyInfo.Instance.MyFriendList)
		{
			FriendListItem item = LTPrefabManager.InstantiateC(PrefabTypes.FriendBar_1, Vector3.zero, Quaternion.identity).GetComponent<FriendListItem>();
			Transform trans = item.transform;
			trans.parent = transform;
			trans.localScale = Vector3.one;
//			CUnitData unitData = CUnitDataManager.Instance.GetUnitData(friendInfo.leaderUnitClassId);
			item.friendUnitImage.spriteName = "Char0"+friendInfo.leaderUnitClassId;
			item.friendName.text = friendInfo.friendName;
			if( friendInfo.friendLevel > 0 )
				item.friendLevel.text = "LV." + friendInfo.friendLevel.ToString();
			else
				item.friendLevel.text = "LV.1";
//			CSkillData skillData = CSkillDataManager.Instance.FindSkillData(friendInfo.skill[0]);
//			if( skillData != null )
//				item.friendSkillIcon[0].spriteName = skillData.icon_name;
//			else
//				item.friendSkillIcon[0].spriteName = "icon_skill_empty";
//			skillData = CSkillDataManager.Instance.FindSkillData(friendInfo.skill[1]);
//			if( skillData != null )
//				item.friendSkillIcon[1].spriteName = skillData.icon_name;
//			else
//				item.friendSkillIcon[1].spriteName = "icon_skill_empty";
			item.index = index;
			LTButtonMessage button = trans.GetComponentInChildren<LTButtonMessage>();
			if( button == null )
				trans.gameObject.AddComponent<LTButtonMessage>();
			button.m_MethodDeliver[0].m_EventListener = this;
			button.m_MethodDeliver[0].m_EventId = index++;
			itemList.Add(item);
		}
	
		GetComponent<UIGrid>().Reposition();
	}
	
	// Update is called once per frame
	void Update () {
	
	}
			
	void ReturnCallback(Hashtable returnData)
	{
		LoadingWindow.window.HideLoading();
		switch((RestAPIProtocol)returnData["protocol"])
		{
		case RestAPIProtocol.GETMYFRIENDLIST:
			RefreshFriendList();
			break;
		case RestAPIProtocol.SENDMYDECKCHANGEINFO:
			if( MyInfo.Instance.MyDeckMap[MyInfo.Instance.MyCurrentDeck].order != 1 )
			{
				LoadingWindow.window.ShowShortLoading();
				MyInfo.Instance.MyDeckMap[MyInfo.Instance.MyCurrentDeck].order = 1;
				RestAPI.singleton.SendMyDeckChangeInfo(ReturnCallback, MyInfo.Instance.MyDeckMap[MyInfo.Instance.MyCurrentDeck].deckId);
			}
			else
			{
				LoadingWindow.window.ShowShortLoading();
				CSceneManager c = GameObject.FindObjectOfType(typeof(CSceneManager)) as CSceneManager;
				c.GoToEnterroomScene(MapListManager.selectedSingleStageMapName, selectedFriendName);
			}
			break;
		}
	}
	
	string selectedFriendName = "";
	public override void OnEvent (int eventID)
	{
		base.OnEvent (eventID);
		
		selectedFriendName = MyInfo.Instance.MyFriendList[eventID].friendName;
//		itemList[eventID].background.spriteName = "UI_btn02s";
//		OnClickStartGameButton();
//		manager.OnClickInviteFriend();
		CSceneManager.manager.SendGameInvite(selectedFriendName);
		manager.ClostSelectFriendWindow();
	}
	
	void OnClickStartGameButton()
	{
		CSceneManager c = GameObject.FindObjectOfType(typeof(CSceneManager)) as CSceneManager;
		LoadingWindow.window.ShowShortLoading();
		c.GoToEnterroomScene(MainSceneManager.selectedSingleMapName, selectedFriendName);
	}
}
